/*
*初始化 流水号码表
 */
package main

import (
	// "net/http"
	// "encoding/json"
	// "strconv"
	// "time"
	// "github.com/dgrijalva/jwt-go"
	// "github.com/labstack/echo"
	"strconv"

	"gopkg.in/mgo.v2/bson"
	// "github.com/jinzhu/gorm"
	"oxy_server/model"
	"oxy_server/module/log"
)

type (
	serialNumber struct {
		Brand  string `json:"brand" bson:"brand"`
		Number string `json:"number" bson:"number"`
		Flag   string `json:"flag" bson:"flag"`
	}
	brand struct {
		ID        bson.ObjectId `json:"_id" bson:"_id"`
		BrandDesc string        `json:"branddesc" bson:"branddesc"`
		BrandCode string        `json:"brandcode" bson:"brandcode"`
		Rule      []string      `json:"rule" bson:"rule"`
	}
)

func main() {
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "brand_dic"
	query, _, err := model.DB().Query(nil, 0, 0)
	if err != nil {
		log.Error("查询出错 品牌列表", err)
		return
	}
	ps := []brand{}
	var data []interface{}
	query.All(&ps)
	for i := 101; i <= 9999; i++ {
		for _, e := range ps {
			num := strconv.Itoa(i)
			if e.BrandDesc == "PN" || e.BrandDesc == "CO" {
				continue
			}
			if len(num) == 3 {
				num = "0" + num
			}
			item := serialNumber{
				Flag:   "0",
				Number: string(num),
				Brand:  e.ID.Hex(),
			}
			data = append(data, item)
		}
	}

	model.DB().Dbname = "OxyDic"
	model.DB().Document = "serialNumber"
	err = model.DB().InsertAll(data...)
	if err != nil {
		log.Error("插入出错", err)
		return
	}

	log.Info("success")
}
